Import mojo
Import agfx

Class TTiled_Layer
	
	Field name:String
	Field properties:TProperties
	Field width:Int
	Field height:Int
	Field tile:TTiled_LayerTile[]
	
	' layer properties
	Field visible:Bool = True
	Field moveable:Bool = True
	Field parallax_scale_x:Float = 1.0
	Field parallax_scale_y:Float = 1.0
	Field scale_x:Float = 1.0
	Field scale_y:Float = 1.0
	Field wrap_x:Bool = False
	Field wrap_y:Bool = False

	'------------------------------------------------------------------------------------
	'
	'------------------------------------------------------------------------------------

	Method New(node:XMLElement)
	
		If DEBUG Then Print("<LAYER>")
		
		Self.properties		= New TProperties()	
		
		Self.name = node.GetAttribute("name")		
		If DEBUG Then Print("      name: "+Self.name)
		
		Self.width = Int(node.GetAttribute("width"))
		If DEBUG Then Print("     width: "+Self.width)
		
		Self.height = Int(node.GetAttribute("height"))
		If DEBUG Then Print("    height: "+Self.height)
		
		Self.tile = New TTiled_LayerTile[Self.width * Self.height]
		Local i:Int = 0
		
		Local layer:XMLElement
		Local tile:XMLElement

		For  layer = EachIn node.Children
		
			Select layer.Name()
				
				Case "data"
					If DEBUG Then Print("     <DATA>")
					For tile = EachIn layer.Children
					
						Select tile.Name()
							Case "tile"
								Self.tile[i] = New TTiled_LayerTile()
													
											
								Local value:String = base_convert(tile.GetAttribute("gid"),10,16)
								
								
								
								If value.Length()>4 Then
									Local flag_hi:String = base_convert(tile.GetAttribute("gid"),10,16)[..4]
								
									Local flag_lo:String = base_convert(tile.GetAttribute("gid"),10,16)[4..]
																
									Self.tile[i].gid = Int(base_convert(flag_lo,16,10))
									
									Self.tile[i].flag = Int(base_convert(flag_hi,16,10))
								Else
									Self.tile[i].flag = 0
									Self.tile[i].gid = Int(tile.GetAttribute("gid"))
								EndIf
								
								if (Self.tile[i].flag & FLAG_SWAP_X)=0 Then 
									
									Self.tile[i].flip_x = 1
								else
									Self.tile[i].flip_x = -1
								EndIf
								
								if (Self.tile[i].gid & FLAG_SWAP_Y)=0 Then 
									
									Self.tile[i].flip_y = 1
								else
									Self.tile[i].flip_y = -1
								EndIF
								
											
								If DEBUG Then Print("      tile gid: "+Self.tile[i].gid)
								i+=1
						End Select
						
						
					Next
				
				Case "properties"
					If DEBUG Then Print(" <PROPERTIES>")
					Self.properties.Add(layer,"property","name","value")
				
			End Select
		Next
			
		' prepare predefined layer properties

		' VISIBLE'
		If Self.properties.Has("visible") Then
			Self.visible = Self.properties.Get("visible").ToBool()
		Else
			Self.visible = True
		EndIf
		
		' MOVEABLE
		If Self.properties.Has("moveable") Then
			Self.moveable = Self.properties.Get("moveable").ToBool()
		Else
			Self.moveable = True
		EndIf

		'PARALLAX X/Y
		If Self.properties.Has("parallax_scale_x") Then
			Self.parallax_scale_x = Self.properties.Get("parallax_scale_x").ToFloat()
		Else
			Self.parallax_scale_x = 1.0
		EndIf

		If Self.properties.Has("parallax_scale_y") Then
			Self.parallax_scale_y = Self.properties.Get("parallax_scale_y").ToFloat()
		Else
			Self.parallax_scale_y = 1.0
		EndIf

		' WRAP X/Y
		If Self.properties.Has("wrap_x") Then
			Self.wrap_x = Self.properties.Get("wrap_x").ToBool()
		Else
			Self.wrap_x = False
		EndIf

		If Self.properties.Has("wrap_y") Then
			Self.wrap_y = Self.properties.Get("wrap_y").ToBool()
		Else
			Self.wrap_y = False
		EndIf

		' SCALE X/Y
		If Self.properties.Has("scale_x") Then
			Self.scale_x = Self.properties.Get("scale_x").ToFloat()
		Else
			Self.scale_x = 1.0
		EndIf

		If Self.properties.Has("scale_y") Then
			Self.scale_y = Self.properties.Get("scale_y").ToFloat()
		Else
			Self.scale_y = 1.0
		EndIf
		
	End Method
	
	'------------------------------------------------------------------------------------
	' Get Tile at X,Y
	'------------------------------------------------------------------------------------
	Method GetTile:TTiled_LayerTile(x:Int,y:Int)
		Return Self.tile[x+y*Self.width]
	End Method
	

	
End Class